package com.noname.lib_base_java.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Author: Lmoumou
 * Date: 2019-01-23 10:11
 * 文件名: EncryptUtils
 * 描述:加密，解密
 */
public class EncryptUtils {
    public static String encodeByMd5(String value) {
        if (value == null || value.isEmpty()) {
            return "";
        }

        byte[] bytes = value.getBytes();
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("md5");
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        if (md5 != null) {
            md5.update(bytes);
            byte[] restltByteArray = md5.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : restltByteArray) {
                if (Integer.toHexString(0xFF & (int) b).length() == 1) {
                    sb.append("0").append(Integer.toHexString(0xFF & (int) b));
                } else {
                    sb.append(Integer.toHexString(0xff & (int) b));
                }
            }
            return sb.toString();
        }

        return "";
    }
}
